package com.csun.dsms.service.impl;

import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.mapper.DsmsAreaMapper;
import com.csun.dsms.mapper.DsmsAreaUnitMapper;
import com.csun.dsms.model.domain.DsmsArea;
import com.csun.dsms.model.domain.DsmsAreaUnit;
import com.csun.dsms.model.vo.AreaUnitVo;
import com.csun.dsms.service.DsmsAreaService;
import com.csun.dsms.service.DsmsAreaUnitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DsmsAreaServiceImpl extends BaseService<DsmsArea> implements DsmsAreaService {

    @Resource
    private DsmsAreaMapper dsmsAreaMapper;
    @Resource
    private DsmsAreaService dsmsAreaServiceImpl;
    @Resource
    private DsmsAreaUnitMapper dsmsAreaUnitMapper;

    /*
     * 片区的添加
     * */
    @Override
    public Integer areaAdd(DsmsArea dsmsArea) {
        List<Long> unitIds = dsmsArea.getUnitIds();
        List<DsmsAreaUnit> dsmsAreaUnits = new ArrayList<>();
        if(unitIds.size()>0){
            for (Long id:unitIds) {
                DsmsAreaUnit dsmsAreaUnit = new DsmsAreaUnit();
                dsmsAreaUnit.setAreaId(dsmsArea.getId());
                dsmsAreaUnit.setUnitId(id);
                dsmsAreaUnits.add(dsmsAreaUnit);
            }
            dsmsAreaUnitMapper.areaUnitSave(dsmsAreaUnits);
        }
        return null;
    }

    /*
     * 片区的查询
     * */
    @Override
    public List<AreaUnitVo> query(DsmsArea dsmsArea) {
        if(dsmsArea !=null && dsmsArea.getId()!=null){
            List<Long> unitIds = dsmsAreaMapper.queryById(dsmsArea);
            List<AreaUnitVo> query = dsmsAreaMapper.query(dsmsArea);
            query.get(0).setUnitIds(unitIds);
            return query;
        }
        List<AreaUnitVo> query = dsmsAreaMapper.query(dsmsArea);
        return query;
    }
    /*
     * 片区的删除
     * */
    @Override
    public Integer deleteArea(DsmsArea dsmsArea) {
        dsmsAreaUnitMapper.deleteAreaUnit(dsmsArea.getId());
        return dsmsAreaMapper.deleteByPrimaryKey(dsmsArea);
    }
    /*
     * 片区的修改
     * */
    @Override
    public Integer updateArea(DsmsArea dsmsArea) {
        dsmsAreaUnitMapper.deleteAreaUnit(dsmsArea.getId());
        List<Long> unitIds = dsmsArea.getUnitIds();
        List<DsmsAreaUnit> dsmsAreaUnits = new ArrayList<>();
        if(unitIds.size()>0){
            for (Long id:unitIds) {
                DsmsAreaUnit dsmsAreaUnit = new DsmsAreaUnit();
                dsmsAreaUnit.setAreaId(dsmsArea.getId());
                dsmsAreaUnit.setUnitId(id);
                dsmsAreaUnits.add(dsmsAreaUnit);
            }
            dsmsAreaUnitMapper.areaUnitSave(dsmsAreaUnits);
        }

        return dsmsAreaServiceImpl.update(dsmsArea);
    }

    /**
     * 根据片区id查询所有单位id
     *
     * @param areaId    the area id
     *
     * @return  the unit id list.
     */
    @Override
    public List<Long> selectUnitIdListByAreaId(Long areaId) {

        return dsmsAreaUnitMapper.selectUnitIdListByAreaId(areaId);
    }

    @Override
    public List<DsmsArea> queryAreaList(Long unitId) {

        DsmsArea dsmsArea = new DsmsArea();
        dsmsArea.setType(1);    // 网格
        dsmsArea.setCompanyId(unitId);

        return dsmsAreaMapper.select(dsmsArea);
    }
    //删除片区或网格时删除与单位的绑定关系
    @Override
    public void deleteAreaUnit(DsmsArea dsmsArea) {
        dsmsAreaMapper.deleteAreaUnit(dsmsArea);
    }
    /*
     * 获取所有片区
     * */
    @Override
    public List<AreaUnitVo> getAllArea() {
        return dsmsAreaMapper.getAllArea();
    }

    //查询该服务人员所在网格
    @Override
    public DsmsArea selectById(Long id) {
        return dsmsAreaMapper.selectById(id);
    }
}
